package com.imooc.miaosha.controller.BackController;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.imooc.miaosha.common.Const;
import com.imooc.miaosha.dto.GoodsDTO;
import com.imooc.miaosha.dto.GoodsManageVO;
import com.imooc.miaosha.service.GoodsService;
import com.imooc.miaosha.service.SolrService;
import com.imooc.miaosha.service.impl.SolrServiceImpl;
import com.imooc.miaosha.util.JsonData;
import org.apache.solr.client.solrj.impl.HttpSolrClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/manage/goods")
public class GoodsManageController {
    @Autowired
    GoodsService goodsService;

    @RequestMapping("/managerList")
    public JsonData list(int pageSize, int pageNum) {
        PageHelper.startPage(pageNum, pageSize);
        List<GoodsManageVO>manageVOList=goodsService.manageList();
        return JsonData.pageableSuccess(goodsService.getTotalCount(),manageVOList);
    }

    @RequestMapping("/add")
    public JsonData add(@RequestBody GoodsDTO goodsDTO) {

        Map<String, Object> respMap = goodsService.add(goodsDTO);
        HttpSolrClient httpSolrClient = new HttpSolrClient.Builder()
                .withBaseSolrUrl(Const.SOLR_BASE_URL)
                .build();
        SolrService solrService = new SolrServiceImpl(httpSolrClient);

        solrService.insert(goodsDTO.getGoodsName(), String.valueOf(respMap.get("incrId")));
        return JsonData.success(respMap);
    }
}
